package com.kotlin.android.ktx.ext

import java.io.*

/**
 *
 * Created on 2020/5/6.
 *
 * @author o.s
 */

fun Closeable?.safeClose() {
    this?.apply {
        try {
            close()
        } catch (e: Throwable) {
        } finally {
        }
    }
}

//fun InputStream?.safeClose() {
//    this?.apply {
//        try {
//            close()
//        } catch (e: Throwable) {
//        } finally {
//        }
//    }
//}

fun OutputStream?.safeClose() {
    this?.apply {
        try {
            close()
        } catch (e: Throwable) {
        } finally {
        }
    }
}

fun Reader?.safeClose() {
    this?.apply {
        try {
            close()
        } catch (e: Throwable) {
        } finally {
        }
    }
}

fun Writer?.safeClose() {
    this?.apply {
        try {
            close()
        } catch (e: Throwable) {
        } finally {
        }
    }
}

/**
 * 遍历获取文件大小
 */
val File.size: Long
    get() {
        var totalSize = 0L
        if (isFile) {
            totalSize = length()
        } else if (isDirectory) {
            listFiles()?.forEach {
                it?.run {
                    totalSize += if (isFile) length() else size
                }
            }
        }
        return totalSize
    }
